/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package newsparser.job;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import newsparser.ConnectorDB;
import newsparser.site.Site;
import newsparser.site.SiteAction;
import org.primefaces.context.RequestContext;

/**
 *
 * @author boyarskiy_km
 */
@ManagedBean
@SessionScoped
public class JobAction implements Serializable{
    private Job selectedJob;
    private Site site;
    private List<SelectItem> sites;
    private String selectedSite;
    private String jobName;
    private int jobTimer;
    private boolean jobStart;
    private String jobJarFile;
    private String jobSource;
    private ConnectorDB cdb = new ConnectorDB();

    
    

    public String getSelectedSite() {
        return selectedSite;
    }
    public void setSelectedSite(String selectedSite) {
        this.selectedSite = selectedSite;
    }
    public String getJobSource() {
        return jobSource;
    }
    public void setJobSource(String jobSource) {
        this.jobSource = jobSource;
    }
    public String getJobJarFile() {
        return jobJarFile;
    }
    public void setJobJarFile(String jobJarFile) {
        this.jobJarFile = jobJarFile;
    }
    public boolean isJobStart() {
        return jobStart;
    }
    public void setJobStart(boolean jobStart) {
        this.jobStart = jobStart;
    }
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
    public int getJobTimer() {
        return jobTimer;
    }
    public void setJobTimer(int jobTimer) {
        this.jobTimer = jobTimer;
    }
    public List<Job> getJobs() {
        List<Job> jobs = new ArrayList<Job>();
        try {
            cdb.rs = cdb.stmt.executeQuery("select * from public.\"VW_JOBS\"");
            while (cdb.rs.next()){
                jobs.add(new Job(cdb.rs.getString("id"),
                                 cdb.rs.getString("name"),
                                 cdb.rs.getString("jar_name"),
                                 cdb.rs.getString("site"),
                                 cdb.rs.getDate("last_working"),
                                 cdb.rs.getInt("timer"),
                                 cdb.rs.getBoolean("iswork")
                ));
                        
            }
            valButtons();
            return jobs;
        } catch (SQLException ex) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } 
    }
    public void addJob(ActionEvent actionEvent){
        FacesMessage msg = null; 
        
        System.err.println("selectedSite = "+selectedSite);
        boolean res = false;
        String text = "{ ? = call \"addjob\"(?,?,?,?,?) }";
        
        
        try {
            if (jobName.equals("")){
                res = false;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", "Не задано наименование");
            }else if (jobTimer == 0){
                res = false;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", "Не задан таймер или таймер равен 0");
            }else{
                cdb.cstmt = cdb.conn.prepareCall(text);
                cdb.cstmt.registerOutParameter(1, Types.VARCHAR);
                cdb.cstmt.setString(2, jobName);
                cdb.cstmt.setInt(3, Integer.valueOf(selectedSite));
                cdb.cstmt.setInt(4, jobTimer);
                cdb.cstmt.setString(5, jobJarFile);
                cdb.cstmt.setString(6, jobSource);        
                cdb.cstmt.execute();
                res = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", selectedSite);
            }
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (SQLException e) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, e);
            System.out.println(e.getMessage());
            res = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", e.getSQLState());
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (NullPointerException e){ 
            System.out.println("asdasd");
        }finally{
            RequestContext.getCurrentInstance().addCallbackParam("res",res);
        }
    }
    public Job getSelectedJob(){
        return selectedJob;
    }
    public void setSelectedJob(Job selectedJob){
        this.selectedJob = selectedJob;
    }      
    public List<SelectItem> getSites() {
        sites = new ArrayList<SelectItem>();
        try {
            cdb.rs = cdb.stmt.executeQuery("select * from public.\"VW_SITES\"");
            while (cdb.rs.next()){
                sites.add(new SelectItem(cdb.rs.getInt("id"),cdb.rs.getString("url")));
            }
            return sites;
        } catch (SQLException ex) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    private void valButtons(){
        try {
            
        } catch (Exception e) {
        }
    }
    
}
